package com.cy.controller;


import com.cy.common.ResultResponse;
import com.cy.config.RequiresRole;
import com.cy.entity.Courses;
import com.cy.service.ICoursesService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  课程管理控制层
 * </p>
 *
 * @author Aleven
 * @since 2024-12-21
 */
@RestController
@RequestMapping("/courses")
public class CoursesController {

    @Autowired
    private ICoursesService iCoursesService ;


    //查询全部课程
    @Operation(summary = "查询所有课程")
    @GetMapping("/FindAllCourses")
    public ResultResponse FindAllCourses(){
        List<Courses> coursesAll = iCoursesService.FindAllCourses() ;
        return  ResultResponse.success(coursesAll,coursesAll.size());
    }


    //根据分类查找课程
    @Operation(summary = "根据分类查找课程")
    @PostMapping("/FindCoursesByClass")
    public ResultResponse FindCoursesByClass(@RequestBody Courses Category){

        List<Courses> coursesClass =iCoursesService.FindCoursesByClass(Category);
        return ResultResponse.success(coursesClass) ;
    }

    //根据条件筛选查找
    @Operation(summary = "根据条件筛选查找")
    @PostMapping("/FindByCondition")
    public ResultResponse FindByCondition(@RequestBody Courses courses){

        List<Courses> coursesCondition = iCoursesService.FindByCondition(courses);
        return ResultResponse.success(coursesCondition,coursesCondition.size());
    }


    //增加课程
    @Operation(summary = "增加课程")
    @PostMapping("/insertCourses")
    @RequiresRole("2")//仅管理员
    ResultResponse insertCourses(@RequestBody Courses courses) throws Exception{
        return iCoursesService.insertCourses(courses);
    }
    //修改课程
    @Operation(summary = "修改课程信息")
    @PutMapping("/updateCourses")
    @RequiresRole("2")//仅管理员
    ResultResponse updateCourese(@RequestBody Courses courses) throws Exception{
        return iCoursesService.updateCourses(courses);
    }


    //删除课程
    @Operation(summary = "删除课程信息")
    @DeleteMapping("/delCourses")
    @RequiresRole("2")//仅管理员
    public String delCourses(String courseID){
        String msg = "error";
        int i = iCoursesService.delCourses(courseID);
        if (i > 0) {
            msg = "success";
        }
        return msg;
    }



}
